package hot100;

import comment.TreeNode;

import java.util.ArrayList;

/**
 * @Author: wzp
 * @CreateTime: 2024-09-25  14:56
 * @Description: 将二叉树转换为链表
 * @Version: 1.0
 */
public class hot_114 {
    public void flatten(TreeNode root) {
        if(root == null){
            return;
        }
        ArrayList<TreeNode> arrayList = new ArrayList<>();
        dfs(root,arrayList);
        for (int i = 0; i < arrayList.size()-1; i++) {
            arrayList.get(i).left = null;
            arrayList.get(i).right = arrayList.get(i+1);
        }
        arrayList.get(arrayList.size()-1).left = null;
        arrayList.get(arrayList.size()-1).right = null;
        return;
    }
    public void dfs(TreeNode root, ArrayList<TreeNode> arrayList)
    {
        if(root == null){
            return;
        }
        arrayList.add(root);
        dfs(root.left,arrayList);
        dfs(root.right,arrayList);
    }
}
